<?php
 
class PhpFox_Mod_Account_ComponentAjaxTopFriends extends BaseComponent
{
    function process()
    {
    	$oSrvFriends = $this->_oModule->getService('Friends');

		$sAddFriend = $this->getParam('add');
		$iForceDisplays = $this->getParam('iForceDisplay');
		$iTotalDisplay = (($iForceDisplays ? $iForceDisplays : (App::getUser('total_friends') ? App::getUser('total_friends') : App::getParam('public.top.friends'))));

		if ( $bRemove = $this->getParam('remove') )
		{
			$this->oDatabase->update(App::getT('friends'), array('top' => '', 'top_order' => ''),
    	    	' (user = "'.$this->oDatabase->escape(App::getUser('user')).'"  AND '.
				'  friend = "'.$this->oDatabase->escape($sAddFriend).'")');		
		}
		else
		{
    		if ( $sAddFriend )
    		{
    			if ( $oSrvFriends->userIsTopFriends(App::getUser('user'), $sAddFriend) )
    			{
    				return 'is_already_top';
    			}
    			elseif ( ($iCntTop = $oSrvFriends->reachedTopLimitFriend(App::getUser('user'))) && $iCntTop >= $iTotalDisplay )
    			{
    				return 'reached_limit';
    			}
    			else
    			{
					$this->oDatabase->update(App::getT('friends'), array('top' => '1', 'top_order' => '6'),
    	        	    ' (user = "'.$this->oDatabase->escape(App::getUser('user')).'"  AND '.
						'  friend = "'.$this->oDatabase->escape($sAddFriend).'")');
				}
    		}
    	}

    	$aFriendsTop = $oSrvFriends->getTopFriendsList(App::getUser('user'), $iTotalDisplay);
    	
    	$sList = '';
    	for ($i = 1; $i <= $iTotalDisplay; $i++)
    	{
    		$sList .= '	<li class="topFriendsNum">' . $i . '</li>' . "\n";
    		if ($i == 6 || $i == 12 || $i == 18 || $i == 24 || $i == 30 || $i == 36)
    		{
    			$iTop = 115;
    			switch ($i)
    			{
    				case 12:
    					$iTop = 230;
    					break;
    				case 18:
    					$iTop = 345;
    					break;    
    				case 24:
    					$iTop = 460;
    					break;
    				case 30:
    					$iTop = 575;
    					break;
    				case 36:
    					$iTop = 690;
    					break;  
    			}
    			
    			$sList .= '</ul>' . "\n";
				$sList .= '<div style="clear:both;"></div>' . "\n";
				$sList .= '</div>' . "\n";
				$sList .= '</div>' . "\n";
				$sList .= '<div style="position:relative;">' . "\n";
				$sList .= '<div style="position:absolute; top:' . $iTop . 'px;">' . "\n";
				$sList .= '<ul style="list-style: none; margin:0px; padding:0px;">' . "\n";
    		}
    	}
    	
    	$iHeight = 810;
  		switch($iTotalDisplay)
  		{
  			case 6:
  				$iHeight = 120;
  				break;
  			case 12:
  				$iHeight = 230;
  				break;
  			case 18:
  				$iHeight = 350;
  				break; 
  			case 24:
  				$iHeight = 460;
  				break; 
  			case 30:
  				$iHeight = 570;
  				break;
  			case 36:
  				$iHeight = 690;
  				break;  
  		}      
    	
    	$this->oTpl->assignSrc('sList', $sList);
    	$this->oTpl->assign(array
    	(
    		'aTop' => $aFriendsTop,
    		'user' => $this->getParam('add'),
    		'iHeight' => $iHeight
    	));
    	
    	return $this->oTpl->fetch('AjaxTopFriends.html');
    }
}

?>